﻿//-----------------------------------------------------------------------
// <copyright file="News.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.ViewModels
{
    using System;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.News;
    using Windows.ApplicationModel.DataTransfer;
    using Windows.System;

    /// <summary>
    /// The view model for the news page
    /// </summary>
    public class News : IShare
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="News"/> class.
        /// </summary>
        public News()
        {
            DataTransferManager.GetForCurrentView().DataRequested += this.News_DataRequested;
            this.Items = new ObservableCollection<NewsStory>(App.RangersNewsFeed.Items);
            this.GoBack = new Command(() =>
            {
                Navigation.GoBack();
            });

            this.RefreshNews = new Command(this.Refresh);
        }

        /// <summary>
        /// Gets or sets the refresh news.
        /// </summary>
        /// <value>The refresh news.</value>
        public Command RefreshNews { get; set; }

        /// <summary>
        /// Gets or sets the go back command.
        /// </summary>
        /// <value>The go back.</value>
        public Command GoBack { get; set; }

        /// <summary>
        /// Gets the items.
        /// </summary>
        /// <value>The items.</value>
        public ObservableCollection<NewsStory> Items { get; private set; }

        /// <summary>
        /// Gets or sets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public NewsStory SelectedItem { get; set; }

        /// <summary>
        /// Should be used to unregister the share event.
        /// </summary>
        public void Unregister()
        {
            DataTransferManager.GetForCurrentView().DataRequested -= this.News_DataRequested;
        }

        /// <summary>
        /// This cause the the news article to launch.
        /// </summary>
        public void NavigateToSelectedItem()
        {
            if (this.SelectedItem.Link != null)
            {
                var launcher = Launcher.LaunchUriAsync(this.SelectedItem.Link);
            }
        }

        /// <summary>
        /// News_s the data requested.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="DataRequestedEventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void News_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            // For twitter sharing
            var projectName = this.SelectedItem.Title + Environment.NewLine;
            var content = this.SelectedItem.Content;

            var titleMaxLength = 140 - content.Length;
            if (titleMaxLength < projectName.Length)
            {
                projectName = projectName.Substring(0, titleMaxLength) + "..." + Environment.NewLine;
            }

            content = projectName + content;
            args.Request.Data.SetText(content);
        }

        /// <summary>
        /// Refreshes this instance.
        /// </summary>
        private async void Refresh()
        {
            await App.RangersNewsFeed.LoadItemsAsync();
            foreach (var newsStory in App.RangersNewsFeed.Items)
            {
                if (!this.Items.Any(item => item.Id == newsStory.Id))
                {
                    this.Items.Add(newsStory);
                }
            }
        }
    }
}